<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 铃声管理系统</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    <style>
        .login-container {
            max-width: 400px;
            margin: 50px auto;
            padding: 30px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
        .login-title {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 25px;
            font-weight: 600;
        }
        .error-message {
            color: #e74c3c;
            margin-top: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1 class="login-title">铃声管理系统 - 登录</h1>
        <form id="loginForm" onsubmit="return login();">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" class="input-field" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" class="input-field" required>
            </div>
            <div class="form-group">
                <button type="submit" class="button" style="width: 100%;">登录</button>
            </div>
            <div id="errorMessage" class="error-message"></div>
        </form>
    </div>

    <script>
        function login() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMessage = document.getElementById('errorMessage');

            if (!username || !password) {
                errorMessage.textContent = '请输入用户名和密码';
                return false;
            }

            fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.location.href = data.redirect_url;
                } else {
                    errorMessage.textContent = data.message || '登录失败，请检查用户名和密码';
                }
            })
            .catch(error => {
                console.error('登录错误:', error);
                errorMessage.textContent = '登录过程中发生错误，请重试';
            });

            return false;
        }
    </script>
</body>
</html>